//
//  MyViewController.m
//  LastTemp
//
//  Created by apple on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import "collectViewController.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "AboutViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MyViewController ()<UIAlertViewDelegate>
@property(nonatomic,retain)UIView *headView;
@property(nonatomic,retain)UIImageView *photo;
@property(nonatomic,retain)UILabel *nameLabel;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *photoURL;
@property(nonatomic,assign)NSInteger dropNum;
@end

@implementation MyViewController

- (void)dealloc
{
    [_headView release];
    [_photo release];
    [_nameLabel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self addTableView];
}


//排行榜点击事件
-(void)PHClick:(UIButton *)button
{
    PHViewController *phVC = [[PHViewController alloc] init];
    [self.navigationController pushViewController:phVC animated:YES];
    [phVC release];
}

-(void)collectClick:(UIButton *)collect
{
    collectViewController *collVC = [[collectViewController alloc] init];
    [self.navigationController pushViewController:collVC animated:YES];
    [collVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark 添加TableView
- (void)addTableView
{
    UIImageView *tableImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:tableImageView];
    [tableImageView release];
    tableImageView.image = [UIImage imageNamed:@"picSky"];
    tableImageView.alpha = 0.5;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64,WIDTH , HEIGHT-49-64)];
    [self.view addSubview:tableView];
    [tableView release];
    tableView.backgroundColor = [UIColor clearColor];
    
    tableView.rowHeight = 70;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self addHeadView];
    tableView.tableHeaderView = self.headView;
    tableView.tag = 500;
    
   
    
    
    
    
}
#pragma mark tableView 必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
     if (indexPath.row == 0) {
        cell.textLabel.text = @"收藏列表";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"清除缓存";
    } else if (indexPath.row == 2) {
        if ([AVSingleton shareSingleton].nightDayNum == 1) {
           cell.textLabel.text = @"日间模式";
        } else {
            cell.textLabel.text = @"夜间模式";
        }
    }  else if (indexPath.row == 3) {
        cell.textLabel.text = @"关于我们";
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (indexPath.row == 0) {
        collectViewController *collVC = [[collectViewController alloc] init];
        [self.navigationController pushViewController:collVC animated:YES];
        [collVC release];
    } else if (indexPath.row == 1) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"将要清除所有缓存信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert show];
        [alert release];
    } else if (indexPath.row == 2) {
        if ([AVSingleton shareSingleton].nightDayNum == 1) {
            [[AVSingleton shareSingleton] day];
            [AVSingleton shareSingleton].nightDayNum = 0;
            UITableView *ta = (UITableView *)[self.view viewWithTag:500];
            [ta reloadData];
        } else {
        [[AVSingleton shareSingleton] night];
            [AVSingleton shareSingleton].nightDayNum = 1;
            UITableView *ta = (UITableView *)[self.view viewWithTag:500];
            [ta reloadData];
        }
    }  else if (indexPath.row == 3) {
        NSLog(@"关于我们");
        AboutViewController *about = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
        [about release];
    }
}
#pragma mark tableView headView
- (void)addHeadView
{
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 180)];
    [_headView release];
    self.headView.backgroundColor = [UIColor redColor];
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    [self.headView addSubview:headImageView];
    [headImageView release];
    headImageView.image = [UIImage imageNamed:@"once2"];
    
    
    
    self.photo = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH/2-50, 20, 100, 100)];
    [self.headView addSubview:self.photo];
    [_photo release];
    self.photo.layer.cornerRadius = 50;
    self.photo.clipsToBounds = YES;
    self.photo.image = [UIImage imageNamed:@"tubiao180"];
    self.photo.backgroundColor = [UIColor whiteColor];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, WIDTH, 30)];
    [self.headView addSubview:self.nameLabel];
    [_nameLabel release];
    self.nameLabel.textAlignment = NSTextAlignmentCenter;


    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *button = [alertView buttonTitleAtIndex:buttonIndex];

    if ([button isEqualToString:@"确定"]) {
        
        NSArray *sandPathBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *str = sandPathBox[0];
        NSFileManager *manger = [NSFileManager defaultManager];
        [manger removeItemAtPath:str error:nil];
        

        [self showMessage:@"缓存已清除"];
        
       }


}
-(void)showMessage:(NSString *)message
{
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-75, self.view.frame.size.height/2-32.5, 150, 65)];
    view2.backgroundColor = [UIColor whiteColor];
    view2.alpha = 0.8;
    UILabel *label = [[UILabel alloc] init];
    label.text = message;
    label.textAlignment = NSTextAlignmentCenter;
    label.frame = CGRectMake(view2.frame.size.width/2-50, view2.frame.size.height/2-20, 100, 40);
    [view2 addSubview:label];
    
    [self.view addSubview:view2];
    [UIView animateWithDuration:2 animations:^{
        view2.alpha = 0;
    } completion:^(BOOL finished) {
        [view2 removeFromSuperview];
    }];
    
    [label release];
    [view2 release];
}


@end
